package com.lib.utils;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.View;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/8/2
 *     desc  : 屏幕相关工具类
 * </pre>
 */
public class ScreenUtils {

	private ScreenUtils() {

		throw new UnsupportedOperationException("u can't instantiate me...");
	}

	/**
	 * 获取当前屏幕截图，包含状态栏
	 *
	 * @param activity
	 * 	activity
	 *
	 * @return Bitmap
	 */
	public static Bitmap captureWithStatusBar(Activity activity) {

		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		DisplayMetrics dm = activity.getResources().getDisplayMetrics();
		Bitmap ret = Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels);
		view.destroyDrawingCache();
		return ret;
	}

	/**
	 * 获取当前屏幕截图，不包含状态栏
	 *
	 * @param activity
	 * 	activity
	 *
	 * @return Bitmap
	 */
	public static Bitmap captureWithoutStatusBar(Activity activity) {

		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		DisplayMetrics dm = activity.getResources().getDisplayMetrics();
		Bitmap ret = Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels);
		view.destroyDrawingCache();
		return ret;
	}

	/**
	 * 判断是否锁屏
	 *
	 * @param context
	 * 	上下文
	 *
	 * @return {@code true}: 是<br>{@code false}: 否
	 */
	public static boolean isScreenLock(Context context) {

		KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
		return km.inKeyguardRestrictedInputMode();
	}
}